package com.dingxin.ship_together_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingxin.ship_together_api.common.utils.PageUtils;
import com.dingxin.ship_together_api.common.utils.SaUtils;
import com.dingxin.ship_together_api.config.exception.CustomException;
import com.dingxin.ship_together_api.entity.ShipInfo;
import com.dingxin.ship_together_api.entity.SysUser;
import com.dingxin.ship_together_api.entity.enums.ExceptionEnum;
import com.dingxin.ship_together_api.entity.vo.PageQueryVo;
import com.dingxin.ship_together_api.mapper.ShipInfoMapper;
import com.dingxin.ship_together_api.service.ShipInfoService;
import com.dingxin.ship_together_api.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Author: ctj
* Date: 2022-09-14
* Time: 11:21
*/

@Service
public class ShipInfoServiceImpl extends ServiceImpl<ShipInfoMapper, ShipInfo> implements ShipInfoService{

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ShipInfoMapper shipInfoMapper;
    @Autowired
    private SaUtils saUtils;


    @Override public boolean save(ShipInfo shipInfo) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq(SysUser.COL_PHONE_NUMBER,shipInfo.getBelongTo()).eq(SysUser.COL_DELETED,false);
        SysUser user = sysUserService.getOne(wrapper);
        if (user == null){
            throw new CustomException(ExceptionEnum.USER_NOT_FOUND);
        }
        return super.save(shipInfo);
    }

    @Override public boolean logicDelete(ShipInfo shipInfo) {
        shipInfo.setDeleted(true);
        return super.updateById(shipInfo);
    }

    @Override public PageUtils selectPage(PageQueryVo pageQueryVo) {
        // 设置分页大小
        Page<ShipInfo> page = new Page<>(pageQueryVo.getCurrPage(),pageQueryVo.getPageSize());
        // 构造分页查询条件
        QueryWrapper<ShipInfo> queryWrapper = new QueryWrapper<>();
        // 分页查询
        Page<ShipInfo> shipInfoPage = shipInfoMapper.selectPage(page, saUtils.generateQueryWrapper(queryWrapper, pageQueryVo));
        // 结果封装
        return new PageUtils(shipInfoPage);
    }
}
